.PHONY : all clean cleanall

METANET_BUILD_PATH ?= ./build

METANET_SRC := metanet_main.c \
			metanet_handle.h metanet_handle.c \
			metanet_start.c metanet_imp.h \
			metanet_malloc.c metanet_malloc.h \
			metanet_module.c metanet_module.h \
			metanet_mq.c metanet_mq.h \
			metanet_server.c metanet_server.h \
			metanet.h

CSERVICE_PATH = $(METANET_BUILD_PATH)/service-lib
CSERVICE = logger

METANET_LIBS := -lm -lrt -ldl -lpthread
SHARED := -fPIC --shared

CFLAGS = -g -o2 -Wall

all : build
build :
	-mkdir -p $(METANET_BUILD_PATH)
	-mkdir -p $(METANET_BUILD_PATH)/service-lib

all : \
    $(METANET_BUILD_PATH)/metanet \
	$(foreach v, $(CSERVICE), $(CSERVICE_PATH)/$(v).so)

$(METANET_BUILD_PATH)/metanet : $(foreach v, $(METANET_SRC), metanet-src/$(v))
	$(CC) $(CFLAGS) -o $@ $^ -Imetanet-src -Imetanet-src -Wl,--whole-archive -Wl,--no-whole-archive $(METANET_LIBS) $(LDFLAGS)

$(CSERVICE_PATH) :
	-mkdir -p $(METANET_BUILD_PATH)/service-lib

define CSERVICE_TEMP
$$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c | $$(CSERVICE_PATH)
	$$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Imetanet-src
endef

$(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v))))

clean :
	rm -f $(METANET_BUILD_PATH)/metanet

cleanall : clean